package Tests;

import static org.junit.Assert.*;
import org.junit.Test;

import example.AliveState;
import example.AliveVisual;
import example.AutomatonImpl;
import example.ConwayCalculator;
import example.DeadState;
import example.DeadVisual;
import example.InitialBoard;
import example.VisualizationPluginImpl;
import framework.Board;
import framework.State;

/**
 * This class tests the plugin
 * 
 * @author jtumarki, ntibrewa, shuaihan, wesleyk
 * 
 */
public class PluginTests {
	
	/**
	 * Tests alive and dead states
	 */
	@Test
	public void StateTests() {
		AliveState as = new AliveState();
		assertTrue(as.getID().equals("ALIVE"));
		
		DeadState ds = new DeadState();
		assertTrue(ds.getID().equals("DEAD"));
	}
	
	/**
	 * Tests alive and dead visuals
	 */
	@Test
	public void AliveVisualTest() {
		AliveVisual av = new AliveVisual();
		assertNotNull(av.getSymbol());
		
		DeadVisual dv = new DeadVisual();
		assertNotNull(dv.getSymbol());
	}
	
	/**
	 * Tests for AutomatonImpl
	 */
	@Test
	public void AutomatonImplTest() {
		//getAllStates test
		AutomatonImpl ai = new AutomatonImpl();
		State[] states = ai.getAllStates();
		assertTrue(states[0] instanceof DeadState);
		assertTrue(states[1] instanceof AliveState);
		
		//getStateCalculator test
		assertTrue(ai.getStateCalculator() instanceof ConwayCalculator);
	}
	
	/**
	 * Tests for ConwayCalculator
	 */
	@Test
	public void ConwayCalculatorTest() {
		State d = new DeadState();
		State a = new AliveState();
		State[][] states = { { d, d, d }, { a, a, d }, {d, d, d} };
		Board b = new Board(3,3,states);
		
		ConwayCalculator calc = new ConwayCalculator();
		assertTrue(calc.getNextState(1,1,b) instanceof DeadState);
		assertTrue(calc.getNextState(0, 0, b) instanceof AliveState);
	}
	
	/**
	 * Tests for InitialBoard
	 */
	@Test
	public void InitialBoardTest() {
		Board b = InitialBoard.createGliderBoard();
		
		assertNotNull(b);
		assertTrue(b.getHeight() == 10);
		assertTrue(b.getWidth() == 10);
	}

	/**
	 * Tests for VisualizationPluginImpl throwing illegal arg exception
	 * with the mapStateToVisual function given a null array
	 */
	@Test(expected = IllegalArgumentException.class)
	public void VisualizationPluginImplMapStateToVisualTest1() {
		AutomatonImpl ai = new AutomatonImpl();
		VisualizationPluginImpl vpi = new VisualizationPluginImpl(ai);
		vpi.mapStateToVisual(null);
	}

	/**
	 * Tests for VisualizationPluginImpl throwing illegal arg exception
	 * with the mapStateToVisual function given an array with a null element
	 */
	@Test(expected = IllegalArgumentException.class)
	public void VisualizationPluginImplMapStateToVisualTest2() {
		AutomatonImpl ai = new AutomatonImpl();
		VisualizationPluginImpl vpi = new VisualizationPluginImpl(ai);
		State[] states = {new AliveState(), new DeadState(), null};
		vpi.mapStateToVisual(states);
	}
	
	/**
	 * Tests for VisualizationPluginImpl throwing illegal arg exception
	 * with the getVisual function
	 */
	@Test(expected = IllegalArgumentException.class)
	public void VisualizationPluginImplGetVisualTest() {
		AutomatonImpl ai = new AutomatonImpl();
		VisualizationPluginImpl vpi = new VisualizationPluginImpl(ai);
		vpi.getVisual(null);
	}
	
}
